﻿using ETA.IRepository;
using ETA.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ETA.Service
{
    public class WebMessageService : IWebMessageService
    {

        #region 属性

        private IWebMessageRepository _IWebMessageRepository { set; get; }


        #endregion

        #region 构造函数

        public WebMessageService(IWebMessageRepository IWebMessageRepository)
        {
            _IWebMessageRepository = IWebMessageRepository;
        }

        #endregion


        public QueryWebMessageResponse QueryWebMessageList(QueryWebMessageRequest req)
        {
            var response = new QueryWebMessageResponse();
            try
            {
                if (!string.IsNullOrEmpty(req.WebMessage.UserId))
                {
                    response.WebMessageList = _IWebMessageRepository.GetAllList(b => b.UserId == req.WebMessage.UserId);
                    response.Result.IsSuccess = true;
                }else
                if (!string.IsNullOrEmpty(req.WebMessage.WebMessageId))
                {
                    response.WebMessageList = _IWebMessageRepository.GetAllList(b => b.WebMessageId == req.WebMessage.WebMessageId);
                    response.Result.IsSuccess = true;
                }
                else
                {
                    response.WebMessageList = _IWebMessageRepository.GetAllList();
                    response.Result.IsSuccess = true;
                }

                if (response.WebMessageList != null && response.WebMessageList.Any())
                {
                    response.WebMessageList = response.WebMessageList.OrderByDescending(m=>m.CreateDate);
                }
            }
            catch (Exception e)
            {
                response.Result.ErrMessage = e.Message;
            }
            return response;
        }

        public CreateWebMessageResponse CreateWebMessage(CreateWebMessageRequest req)
        {
            var res = new CreateWebMessageResponse();
            try
            {
                if (req != null)
                {
                    res.WebMessage = _IWebMessageRepository.Insert(req.WebMessage);
                    res.Result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                res.Result.IsSuccess = false;
                res.Result.ErrMessage = e.Message;
            }
            return res;
        }

        public ModifyWebMessageResponse ModifyWebMessageService(ModifyWebMessageRequest req)
        {
            var res = new ModifyWebMessageResponse();
            try
            {
                if (req != null)
                {
                    res.WebMessage = _IWebMessageRepository.Update(req.WebMessage);
                    res.Result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                res.Result.IsSuccess = false;
                res.Result.ErrMessage = e.Message;
            }
            return res;
        }
    }
}
